﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator.Console
{
    public class CodeGeneratorCore
    {
        int userId = -1;
        string userName = "";

        public void Process()
        {
            if (!Authen())
            {
                System.Console.WriteLine("Authentication Fail.");
                return;
            }

            bool loop = true;

            while(loop)
            {
                IAction action = GetUserAction();

                if (action is IGeneratorAction)
                {
                    String outputPath = GetOutputPath();
                    int projectId = GetProjectId();
                    ((IGeneratorAction)action).GenerateOutput(projectId, outputPath);
                }

                if (action is IDisplayableAction)
                    ((IDisplayableAction)action).Display(userId);

                if (action is SystemExitAction)
                    loop = false;
                else
                {
                    System.Console.WriteLine("Press any key to Continue...");
                    System.Console.ReadKey();
                    System.Console.WriteLine("");
                }
            }
        }

        private String GetOutputPath()
        {
            System.Console.Write("Enter OutputPath : ");
            try
            {
                var input = Convert.ToString(System.Console.ReadLine());
                return input;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
                return "";
            }
        }

        private int GetProjectId()
        {
            System.Console.Write("Enter ProjectId : ");
            try
            {
                var input = Convert.ToInt32(System.Console.ReadLine());
                return input;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex);
                return -1;
            }
        }

        private IAction GetUserAction()
        {
            //System.Console.Clear();
            System.Console.WriteLine("###############   Please Select Command Below to Operate  ###############");
            System.Console.WriteLine("1. Display Project List.");
            System.Console.WriteLine("2. Display Class List.");
            System.Console.WriteLine("3. Generate Complex MVC3 Web Application.");
            System.Console.WriteLine("0. Exit.");
            System.Console.Write("=====> ");
            var result = System.Console.ReadLine();
            result = result.Trim().Replace(".", "");
            while (true)
            {
                switch (result)
                {
                    case "1":
                        return new DisplayProjectAction();
                    case "2":
                        return new DisplayClassAction();
                    case "3":
                        return new GenMVC3Action();
                    case "0":
                        return new SystemExitAction();
                }

                System.Console.WriteLine("Invalid Command.");
                System.Console.WriteLine("===>");
                result = System.Console.ReadLine();
                result = result.Trim().Replace(".", "");
            }
        }

        private bool Authen()
        {
            System.Console.WriteLine("Username and Password are case sensitive...");
            System.Console.Write("Username: ");
            var username = System.Console.ReadLine();
            System.Console.Write("Password: ");
            var password = System.Console.ReadLine();
            ActionResult result = new VerifyUserAction().Verify(username, password);
            userId = result.Id;
            userName = result.Code;
            return result.Status;
        }
    }
}
